﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class EffectMgr : MonoSingleton<EffectMgr>
{

    public enum EffectType
    {
        none,
        simple_particles,
        simple_audiovisual,
        custom,//自定义，不使用内置的特效，而是使用bundle中提供的特效
        Max
    }

    const string effectBoundleName = "effect";

    EffectType effecttype;
    int cureffecttype_index;
    Transform last_effect;
    Transform anchor;
    ProcessMusic processMusic;
    PackageData configData;

    public ProcessMusic ProcessMusic{
        get{ return processMusic; }
    }

    protected override void Init()
    {
        base.Init();
        EventBus.Instance.AddEventHandler<EffectType>(EventID.EffectChangeEvent, ChangeEffect);
    }

    void Start()
    {
        processMusic = new ProcessMusic();
        processMusic.Init();
        anchor = transform.Find("anchor");
    }

    public void OnPackageChange()
    {
        //切换了package后这里需要重新获取
        configData = AppDataManager.Instance.GetCurrentPackageInfo();
        ChangeEffect(configData.packageConfig.effecttype);
    }

    void Update()
    {

        //切换了package后这里需要重新获取
        configData = AppDataManager.Instance.GetCurrentPackageInfo();

        //特效总开关
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (last_effect != null)
            {
                last_effect.SetActive(!last_effect.gameObject.activeInHierarchy);
            }
            else
            {
                ChangeEffect(configData.packageConfig.effecttype);
            }

            AppConfig.Instance.AppConfigData.isEnableEffect = !AppConfig.Instance.AppConfigData.isEnableEffect;
            AppManager.Instance.Tip = "特效开关：" + AppConfig.Instance.AppConfigData.isEnableEffect;
        }

        //音频可视化
        if (configData.packageConfig.isSupportAudioVisual)
        {
            EventBus.Instance.BroadCastEvent<float>(EventID.OnAudioAmpUpdate, processMusic.AudioAmp);
            processMusic.OnUpdate();
            if (last_effect != null)
            {
                EffectBase effectBase = last_effect.GetComponentInChildren<EffectBase>();
                if (effectBase != null)
                {
                    effectBase.OnUpdateAmp(processMusic.AudioAmp * configData.packageConfig.audioVisulStrengh);
                }
            }
        }
    }

    private void NextEffect()
    {
        cureffecttype_index++;
        if (cureffecttype_index >= (int)EffectType.Max)
        {
            cureffecttype_index = 0;
        }
        effecttype = (EffectType)cureffecttype_index;
        ChangeEffect(effecttype);
        configData.packageConfig.effecttype = effecttype;

        AppManager.Instance.Tip = "特效名称：" + effecttype.ToString();
    }

    public void ChangeEffect(EffectType effecttype)
    {
        //销毁之前的特效
        if (last_effect != null)
        {
            Debug.Log(last_effect);
            Destroy(last_effect.gameObject);
        }

        //自定义特效（ab包中提供）
        if (effecttype == EffectType.custom)
        {
            CreateNewEffectWithBundle();
        }
        else
        {
            Res.LoadGameObjectAsync("Prefabs/effects/" + effecttype.ToString(), (obj) =>
            {
                GameObject effectGameObject = Instantiate<GameObject>(obj, transform);
                effectGameObject.transform.position = anchor.position;
                last_effect = effectGameObject.transform;
            });
        }
    }

    private void CreateNewEffectWithBundle()
    {
        UnityEngine.Object obj = PaperManager.Instance.GetAssetFromCurrentAssetBundle("effect");
        if (obj != null)
        {
            GameObject effectGameObject = Instantiate<GameObject>(obj as GameObject, transform);
            effectGameObject.transform.position = anchor.position;
            last_effect = effectGameObject.transform;
        }
    }

    protected override void OnDestroy()
    {
        base.OnDestroy();
        processMusic.Stop();
    }

}
